{% extends 'myadmin/index.html'%}

{% block title %}
<title>后台新闻添加</title>
{% endblock %}

{% block con %}

<div class="row">
        <div class="am-u-sm-12 am-u-md-12 am-u-lg-12">
            <div class="widget am-cf">
                <div class="widget-head am-cf">
                    <div class="widget-title am-fl">
                        添加新闻
                    </div>
                    <div class="widget-function am-fr">
                        <a href="javascript:;" class="am-icon-cog">
                        </a>
                    </div>
                </div>
                <div class="widget-body am-fr">

                    <form action="{% url 'goods_insert' %}" method="POST" enctype="multipart/form-data" class="am-form tpl-form-line-form">
                        {% csrf_token%}
                        <div class="am-form-group">
                            <label for="user-name" class="am-u-sm-3 am-form-label">新闻所属分类</label>
                            <div class="am-u-sm-9">
                                <select name="cateid" data-am-selected="{btnSize:'sm'}" id="" style="display:none;">
                                    {% for v in context %}
                                    <!--输出所选的id 如果pid是0 则是顶级分类 不可选中-->
                                    <option value="{{v.id}}"   {% if v.pid == 0 %} disabled {% endif %}>{{v.sub}}{{v.name}}</option>
                                    {% endfor %}
                                </select>
                            </div>
                        </div>
                        <div class="am-form-group">
                            <label for="user-name" class="am-u-sm-3 am-form-label">新闻名称</label>
                            <div class="am-u-sm-9">
                                <input type="text" name="goodsname" class="tplf-form-input" placeholder="请输入新闻名称">
                            </div>
                        </div>
                        <div class="am-form-group">
                            <label for="user-name" class="am-u-sm-3 am-form-label">新闻标题</label>
                            <div class="am-u-sm-9">
                                <input type="text" name="title" class="tplf-form-input" placeholder="请输入新闻标题">
                            </div>
                        </div>
                        <!--<div class="am-form-group">-->
                            <!--<label for="user-name" class="am-u-sm-3 am-form-label">新闻价格</label>-->
                            <!--<div class="am-u-sm-9">-->
                                <!--<input type="number" name="price" class="tplf-form-input" placeholder="请输入价格">-->
                            <!--</div>-->
                        <!--</div>-->
                        <!--<div class="am-form-group">-->
                            <!--<label for="user-name" class="am-u-sm-3 am-form-label">新闻库存</label>-->
                            <!--<div class="am-u-sm-9">-->
                                <!--<input type="number" name="goodsunm" class="tplf-form-input" placeholder="请输入库存">-->
                            <!--</div>-->
                        <!--</div>-->
                        <div class="am-form-group">
                            <label for="user-weibo" class="am-u-sm-3 am-form-label">
                                上传头像
                                <span class="tpl-form-line-small-title">
                                    Images
                                </span>
                            </label>
                            <div class="am-u-sm-9">
                                <div class="am-form-group am-form-file">
                                    <div class="tpl-form-file-img">
                                        <!--<img src="/static/myadmin/assets/img/a5.png" alt="">-->
                                         <img src="" alt="" id="showImg" width="100">
                                    </div>
                                    <button type="button" class="am-btn am-btn-danger am-btn-sm">
                                        <i class="am-icon-cloud-upload">
                                        </i>
                                        添加图片
                                    </button>
                                    <!--<input id="doc-form-file" name="pic" type="file" multiple="">-->
                                    <input type="hidden" id="img" />
		                            <input type="file" id="uploadImg" onchange="selectImg(this);" name="pic" multiple=""/>
                                </div>
                            </div>
                        </div>
                            <link rel="stylesheet" type="text/css" href="/ueditor/UE/third-party/SyntaxHighlighter/shCoreDefault.css">
    <script type="text/javascript" src="/ueditor/UE/third-party/SyntaxHighlighter/shCore.js"></script>
    <script type="text/javascript" src="/ueditor/UE/ueditor.config.js"></script>
    <script type="text/javascript" src="/ueditor/UE/ueditor.all.min.js"></script>
    <script type="text/javascript" src="/ueditor/UE/lang/zh-cn/zh-cn.js"></script>


    <div class="am-form-group">
        <label for="user-intro" class="am-u-sm-3 am-form-label">新闻简介</label>
        <div class="am-u-sm-9">
            <!-- <textarea name="descr" class="" rows="10" id="user-intro" placeholder="请输入新闻简介"></textarea> -->
            <!-- <script id="editor" type="text/plain" style="width:100%;height:500px;"></script> -->
            <script id="editor" name="goodsinfo" type="text/plain" style="height:500px;color:#000"></script>
        </div>
    </div>


    <script type="text/javascript">
        var ue = UE.getEditor('editor');
        SyntaxHighlighter.all();
    </script>
                        <div class="am-form-group">
                            <div class="am-u-sm-9 am-u-sm-push-3">
                                <button class="am-btn am-btn-primary tpl-btn-bg-color-success ">
                                    提交
                                </button>
                            </div>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
<script>
 $(document).ready(function(){
   $('#shangpinguanli').click()
})
var image = '';
	var canvas;
	var base64; //将canvas压缩为base64格式
	function selectImg(file) {
		if (!file.files || !file.files[0]) {
			return;
		}
		var reader = new FileReader(); //读取文件
		reader.onload = function(event) { //文件读取完成的回调函数
			image = document.getElementById('showImg');
			image.src = event.target.result; //读入文件的base64数据(可直接作为src属性来显示图片)
			//alert(event.target.result);
			//图片读取完成的回调函数（必须加上否则数据读入不完整导致出错！）
			image.onload = function() {
				canvas = convertImageToCanvas(image); //图片转canvas
				base64 = canvas.toDataURL('image/jpeg'); //将图片数据转为base64.
				//alert(base64);
				$.post(
					"/server_interface_url/", //服务器接口(返回图片路径)
					{
						data: base64
					},
					function(data) {
						alert(data.target);
						//alert(eval(data));
						//修改上传文件的路径名字(图片完整路径)
						$('#img').val('http://path/' + data.target);
					}, "json");
			}
		}
		//将文件已Data URL的形式读入页面
		reader.readAsDataURL(file.files[0]);
	}
	// 把image 转换为 canvas对象
	function convertImageToCanvas(image) {
		// 创建canvas DOM元素，并设置其宽高和图片一样
		var canvas = document.createElement("canvas");
		canvas.width = image.width;
		canvas.height = image.height;
		// 坐标(0,0) 表示从此处开始绘制，相当于偏移。
		canvas.getContext("2d").drawImage(image, 0, 0);

		return canvas;
	}
</script>
{% endblock %}

